package login;

import java.io.IOException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2.domain.Categoria;
import swimv2.domain.Utente;
import swimv2.session.LoginRemoteInterface;

public class RegistrazioneServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2031060609642034952L;


	
	public RegistrazioneServlet() throws NamingException{
		super();
	}
	
	public static Context getInitialContext() throws NamingException {

		Properties prop = new Properties();
		System.out.println("context");
		prop.setProperty("java.naming.factory.initial",
				"org.jnp.interfaces.NamingContextFactory");
		prop.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
		prop.setProperty("java.naming.provider.url", "127.0.0.1:1099");
		return new InitialContext(prop);

		
	}
	LoginRemoteInterface LRI;
	Categoria cat = new Categoria();
	Utente u = new Utente();
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Context ctx = null;
		try {
			ctx = getInitialContext();
		} catch (NamingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			LRI = (LoginRemoteInterface) ctx.lookup("LogicaLogin/remote");
		} catch (NamingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if(request.getParameter("nome").equals("popola"))
			popola();
		else{
		
		try {

		
			cat=LRI.getCategoria("Giardinaggio");
			u = LRI.inserisciUtente(request.getParameter("nome"),request.getParameter("cognome"), request.getParameter("citta"), request.getParameter("email"), request.getParameter("nomeutente"), request.getParameter("password"));
			LRI.inserisciAbilita(u,request.getParameter("abilita"), cat);
		}
			catch(Exception e){
				System.out.println("ERRORE REGISTRAZIONE");
			}
		}
			request.getRequestDispatcher( "index.jsp" ).forward( request, response );
	}
	
	public void popola(){
		
		
		LRI.inserisciAmministratore("marco", "rossi", "email", "marco", "rossi");
		LRI.inserisciAmministratore("mario", "blu", "gmail", "marietto", "blu");
		LRI.inserisciCategoria();
		u = LRI.inserisciUtente("giuseppe", "verdi", "milano", "gv", "beppe", "verdi");
		cat = LRI.getCategoria("Giardinaggio");
		LRI.inserisciAbilita(u,"Potare", cat);
		u=LRI.inserisciUtente("vittorio", "emanuele", "milano", "ema", "vitto", "vittema");
		cat = LRI.getCategoria("Computer");
		LRI.inserisciAbilita(u,"Programmare", cat);

	}
}


